package org.book.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.book.dao.UserBean;
import org.book.service.UserService;

/**
 * Servlet implementation class UserUpdateServlet
 */
@WebServlet("/UserUpdateServlet")
public class UserUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		UserService userService = new UserService();
		
		String pageCurrent2 = request.getParameter("pageCurrent2");
		
		String no = request.getParameter("no");
		String userName = request.getParameter("userName");
		String userNumber = request.getParameter("cardNumber");
		
		if(no!=null&&userName!=null&&userNumber!=null){
			
			int id = Integer.parseInt(no);
			UserBean stu = new UserBean(userName,userNumber);
			//添加
			if(id==-1) {
				
				boolean bool = userService.add(stu);
				
				if(bool) {
					System.out.println("添加成功");
					response.sendRedirect("UsersServlet");
					//request.getRequestDispatcher("UsersServlet").forward(request, response);
				}else {
					System.out.println("添加失败");
					request.getRequestDispatcher("UsersServlet").forward(request, response);
				}
			}
			//修改
			else {
				
				boolean bool = userService.update(id, stu);
				
				if(bool) {
					System.out.println("修改成功");
					//保存页码
					request.setAttribute("pageCurrent2", pageCurrent2);
					request.getRequestDispatcher("UsersServlet").forward(request, response);
				}else {
					System.out.println("修改失败");
					request.getRequestDispatcher("UsersServlet").forward(request, response);
				}
			}
			
			
			
		}
		//删除
		else if(no!=null&&userName==null){
			int id = Integer.parseInt(no);
			boolean bool = userService.remove(id);
			if(bool) {
				System.out.println("删除成功");
				//保存页码
				request.setAttribute("pageCurrent2", pageCurrent2);
				request.getRequestDispatcher("UsersServlet").forward(request, response);
			}else {
				System.out.println("删除失败");
				request.getRequestDispatcher("UsersServlet").forward(request, response);
			}
		}
		//错误
		else {
			System.out.println("输入错误");
		}
		
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
